package com.myway.service.impl;

import com.myway.dao.TabIntegralRecordMapper;
import com.myway.dao.TabMembIntegralMapper;
import com.myway.dto.common.JsonData;
import com.myway.dto.param.IntegralParam;
import com.myway.dto.util.OrderNumberGenerator;
import com.myway.pojo.TabIntegralRecord;
import com.myway.pojo.TabMembIntegral;
import com.myway.service.TabMembIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TabMembIntegralServiceImpl
 * @Description: 积分实现类
 * @Auther: MaShuai
 * @Date: 2018/9/18 12:27
 * @Version 1.0
 */
@Service
public class TabMembIntegralServiceImpl implements TabMembIntegralService {

    @Autowired
    private TabMembIntegralMapper tabMembIntegralMapper;

    @Autowired
    private TabIntegralRecordMapper tabIntegralRecordMapper;


    @Override
    public JsonData addMembIntegral(TabMembIntegral tabMembIntegral, TabIntegralRecord tabIntegralRecord) {
        if(tabMembIntegral != null && tabIntegralRecord != null){
            tabMembIntegral.setIntegralId(OrderNumberGenerator.getOrderNo());
            int integralAddNum = tabMembIntegralMapper.insertSelective(tabMembIntegral);
            tabIntegralRecord.setIntegralId(tabMembIntegral.getIntegralId());
            int recordNum = tabIntegralRecordMapper.insertSelective(tabIntegralRecord);
            if(integralAddNum + recordNum == 2){
                return JsonData.success();
            } else {
                return JsonData.fail("Add failed!");
            }

        } else {
            return JsonData.fail("The param is null!");
        }

    }

    @Override
    public JsonData updateMembIntegralById(TabMembIntegral tabMembIntegral, TabIntegralRecord tabIntegralRecord) {
        if(tabMembIntegral.getIntegralId() != null){
            int i = tabMembIntegralMapper.updateByPrimaryKeySelective(tabMembIntegral);
            TabIntegralRecord old = tabIntegralRecordMapper.findRecordByIntegralId(tabMembIntegral.getIntegralId());
            tabIntegralRecord.setId(old.getId());
            int r = tabIntegralRecordMapper.updateByPrimaryKeySelective(tabIntegralRecord);
            if(i + r == 2){
                return JsonData.success();
            } else {
                return JsonData.fail("Update failed!");
            }

        } else {
            return JsonData.fail("The getIntegralId is null!");
        }
    }

    @Override
    public JsonData selectMembIntegralByMembId(String membId) {
        if(membId != null && membId != ""){
            List<TabMembIntegral> tabMembIntegralList = tabMembIntegralMapper.findByMembId(membId);
            return JsonData.success(tabMembIntegralList);
        } else {
            return JsonData.fail("The membId is null!");
        }

    }

    @Override
    public JsonData selectMembIntegralByParam(IntegralParam param) {
        String membId = param.getMembId();
        if(membId != null && membId != ""){
            List<TabMembIntegral> tabMembIntegralList = tabMembIntegralMapper.selectByIntegralParam(param);
            return JsonData.success(tabMembIntegralList);
        } else {
            return JsonData.fail("The membId is null!");
        }
    }

    @Override
    public JsonData delMembIntegral(TabMembIntegral tabMembIntegral, TabIntegralRecord tabIntegralRecord) {
        if(tabMembIntegral != null && tabIntegralRecord != null){
            int integralUpdNum = tabMembIntegralMapper.updateByPrimaryKeySelective(tabMembIntegral);
            int recordNum = tabIntegralRecordMapper.insertSelective(tabIntegralRecord);
            if(integralUpdNum + recordNum == 2){
                return JsonData.success();
            } else {
                return JsonData.fail("Add failed!");
            }

        } else {
            return JsonData.fail("The param is null!");
        }
    }

    @Override
    public List<TabMembIntegral> getIntegralInfo(String orderId) {
        return tabMembIntegralMapper.findByOrderId(orderId);
    }
}
